package com.net.xpay.web.controller.base;

import com.google.common.base.Preconditions;
import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.LoginLog;
import com.net.xpay.common.manager.LoginLogManager;
import com.net.xpay.core.model.LoginLogSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.web.secutiry.CurrentOperator;
import com.net.xpay.web.secutiry.OperatorStatus;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.model.PasswordModel;
import com.net.xpay.common.service.UserService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.UserAccount;
import com.net.xpay.core.manager.UserAccountManager;
import com.net.xpay.web.secutiry.Operator;
import com.net.xpay.web.secutiry.RequestUser;
import com.net.xpay.web.vo.LoginLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Created by net on 2018/3/15.
 * 用户账户控制类,充值，提现等
 */
@RestController
@RequestMapping("/base/user-account")
@Api(tags = "基础/用户账户", description = "基础 商户账户 net")
public class UserAccountController {

    @Autowired
    private UserAccountManager userAccountManager;

    @Autowired
    private UserService userService;
    @Autowired
    private LoginLogManager loginLogManager;

    @ApiOperation(value = "获取账户")
    @GetMapping
    public ResultData<UserAccount> getAccount(@ApiIgnore @RequestUser User user) {
        UserAccount account = userAccountManager.getById(user.getId());
        return new ResultData<>(account);
    }

    @ApiOperation(value = "修改支付密码")
    @PutMapping("/modify-pay-password")
    public ResultData<Boolean> modifyPayPassword(@ApiIgnore @CurrentOperator Operator operator, @ApiIgnore @RequestUser User user, @RequestBody PasswordModel passwordModel) {
        if (operator.getStatus() == OperatorStatus.OPEN_ID_NEED_BIND) {
            throw new BusinessException(PoseidonErrorCode.OPEN_ID_NEED_BIND, "请先绑定账号,绑定之后才能修改支付密码");
        }

        return new ResultData<>(userService.modifyPayPassword(user, passwordModel) != null);
    }



    /**
     * 列表
     * @return
     */
    @ApiOperation("列表")
    @PostMapping("/login-page-search")
    public ResultPagedList<LoginLogVo> search(@ApiIgnore @RequestUser User merchant, @RequestBody LoginLogSearchModel loginLogSearchModel) {
        Preconditions.checkArgument(merchant != null);
        loginLogSearchModel.setUserId(merchant.getId());
        List<LoginLog> records = loginLogManager.search(loginLogSearchModel);
        Long count = loginLogManager.searchCount(loginLogSearchModel);
        List<LoginLogVo> vos = BeanCopierUtil.batchCopy(LoginLog.class, LoginLogVo.class, records);
        return new ResultPagedList<LoginLogVo>(vos, count, loginLogSearchModel);
    }
}
